/**
 * TmCustomServiceImpl.java
 * Created at 2017-2-8
 * Created by xieshuang
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.StringUtils;
import com.broadtext.rms.basic.dao.ITmCustomDao;
import com.broadtext.rms.basic.model.TmCustom;
import com.broadtext.rms.basic.service.ITmCustomService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;


/**
 * <p>ClassName: TmCustomServiceImpl</p>
 * <p>Description: 客户服务层实现类</p>
 * <p>Author: xieshuang</p>
 * <p>Date: 2017-2-8</p>
 */
@Service("tmCustomService")
public class TmCustomServiceImpl extends EntityServiceImpl<TmCustom> implements ITmCustomService {
    /**
     * 
     * <p>Description: 基类dao</p>
     * @param entityDao 基类实体dao
     */
    @Autowired
    @Qualifier("tmCustomDao")
    public void setEntityDao(EntityDao<TmCustom> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * 注入dao
     */
    @Autowired
    @Qualifier("tmCustomDao")
    private ITmCustomDao tmCustomDao;
    /**
     * 
     * <p>Description: 批量删除 </p>
     * @param idArray 主键数组
     * @param currentUser 当前登录用户名
     * @throws DaoException 异常
     */
    public void delete(String[] idArray, String currentUser) throws DaoException {
        //循环删除 
        for (String id : idArray) {
            TmCustom t;
            t = new TmCustom();
            t.setPkId(id);
            t.setUpdateDate(new Date());
            t.setUpdateUser(currentUser);
            t.setDelFlag("0");
            this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmCustom.deleteRecord", t);
        }
    }
    
    /**
     * 
     * <p>Description: 客户字典查询</p>
     * @param queryMao 查询条件
     * @return 客户字典
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getCustomDict(Map<String, Object> queryMap) throws DaoException {
        return this.entityDao.findByQuery("com.broadtext.rms.basic.model.TmCustom.findAllCustom", queryMap);
    }
    
    /**
     * 
     * <p>Description: 根据id获取客户对象</p>
     * @param id 主键id
     * @return 客户对象
     * @throws DaoException 异常
     */
    public TmCustom findCustomerById(String id) throws DaoException {
        return this.tmCustomDao.findCustomerById(id);
    }
    
    /**
     * 
     * <p>Description: 检查待删除客户状态是否合法</p>
     * @param ids id数组
     * @return 检查结果
     * @throws DaoException 异常
     */
    public Object checkCustomerStatus(String[] ids) throws DaoException {
        return this.tmCustomDao.checkCustomerStatus(ids);
    }
    
    /**
     * 
     * <p>Description: 查询客户简称列表</p>
     * @return 客户
     * @throws ServiceException 异常
     */
    @SuppressWarnings("unchecked")
	public List<Object> getCustomerShortNames() throws ServiceException {
    	List<Object> list;
        list = new ArrayList<Object>();
        try {
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findCustomShortNameOpt", null);
        } catch (DaoException e) {
            throw new ServiceException(e);
        }
        return list;
    }
    
    /**
     * 
     * <p>
     * Description: 根据parentKey获取字典详情
     * </p>
     * 
     * @param parentKey parentKey
     * @return 字典详情
     * @throws DaoException
     */
    @SuppressWarnings({"unchecked"})
    public List<Object> getParentCusOption(String parentId,String operation) throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        List<Map<String,Object>> cusIdList;
        cusIdList = new ArrayList<Map<String,Object>>();
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        List<String> ids = new ArrayList<String>() ;
        if(StringUtils.isNotBlank(parentId)){
            queryMap.put("rootId", parentId);
        }
        try {
            //查询本级和本下级客户id
            cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", queryMap);
            for (Map<String,Object> map : cusIdList) {
                ids.add(map.get("PK_ID")+"");
            }
			
			//代码走一个差，顶一个update方法，然后来个注释
            if("update".equals(operation)){
                list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.getParentCusOptionExcludeSelf", ids);
            }else {
                list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.getChildCusOption", ids);
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }
    
   /**
    * 
    * <p>Description: 编辑客户时获取上级可选单位去除客户本身</p>
    * @param rootId 登录人pk_id 根节点id，根据此id获取客户树形结构数据
    * @param cusId 需要编辑的用户pk_id,选上级单位时不能选自己作为自己的上级单位，且不能选自己的子节点单位作为自己的上级单位
    * @return
    * @throws ServiceException
    */
    @SuppressWarnings({"unchecked"})
    public List<Object> getParentCusOptionExcludeSelf(String rootId,String cusId) throws ServiceException {
        List<Object> list;
        list = new ArrayList<Object>();
        List<Map<String,Object>> cusIdList;
        cusIdList = new ArrayList<Map<String,Object>>();
        List<Map<String,Object>> childCusIdList;
        childCusIdList = new ArrayList<Map<String,Object>>();
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        Map<String, Object> paramMap;
        paramMap = new HashMap<String, Object>();
        List<String> ids = new ArrayList<String>() ;
        if(StringUtils.isNotBlank(rootId)){
            queryMap.put("rootId", rootId);
        }
        if(StringUtils.isNotBlank(cusId)){
            paramMap.put("rootId", cusId);
        }
        try {
            //查询登录用户本级和本下级客户id
            //若rootId为空，则表示为非客户类型账户，属于系统管理员，查询所有客户节点，否则查询当前登录账户子节点所有账户
            if(StringUtils.isBlank(rootId)){
                cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findAllCustomOption",paramMap);
            }else {
                cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", queryMap);
            }
            //查询当前被编辑客户节点PK_ID和子节点PK_ID
            childCusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", paramMap);  
            //去重，被编辑客户选择上级单位不能选自己本身且不能选自己的子节点单位
            for (Map<String,Object> map : cusIdList) {
                boolean flag = true;
                for (Map<String,Object> childMap : childCusIdList) {
                    if(String.valueOf(map.get("PK_ID")).equals(String.valueOf(childMap.get("PK_ID")))){
                        flag = false;
                    }
                }
                if(flag){
                    ids.add(map.get("PK_ID")+"");
                }
            }
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.getChildCusOption", ids);
            
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        return list;
    }
    
    /**
     * 
     * <p>Description: 根据根节点获取子节点pk_id 数组</p>
     * @param rootId
     * @return
     * @throws ServiceException
     */
    @SuppressWarnings({"unchecked"})
    public List<String> getChildCusIdArray(String rootId) throws ServiceException {
        List<String> ids = new ArrayList<String>() ;
        Map<String, Object> queryMap;
        queryMap = new HashMap<String, Object>();
        List<Map<String,Object>> cusIdList;
        cusIdList = new ArrayList<Map<String,Object>>();
        if(StringUtils.isNotBlank(rootId)){
            queryMap.put("rootId", rootId);
        }
        //查询本级和本下级客户id
        try {
            cusIdList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmCustom.findTreeCusIds", queryMap);
            for (Map<String,Object> map : cusIdList) {
                ids.add(map.get("PK_ID")+"");
            }
        } catch (DaoException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
        
        return ids;
    }

    /** (non-Javadoc)
     * <p>Title: getCustomerOptions</p>
     * <p>Description: </p>
     * @param params
     * @param pageBounds
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.basic.service.ITmCustomService#getCustomerOptions(java.util.Map, com.github.miemiedev.mybatis.paginator.domain.PageBounds)
     */
    @Override
    public EntityPageBean<TmCustom> getCustomerOptions(Map<String, Object> params, PageBounds pageBounds)
            throws ServiceException {
        try {
            return this.entityDao.findEntityPageByQuery("com.broadtext.rms.basic.model.TmCustom.getCustomerOptions", params, pageBounds);
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return null;
    }

    /** (non-Javadoc)
     * <p>Title: initBatteryCorrelation</p>
     * <p>Description: 获取客户关联电池</p>
     * @param id
     * @return
     * @throws ServiceException
     * @see com.broadtext.rms.basic.service.ITmCustomService#initBatteryCorrelation(java.lang.String)
     */
    @Override
    public Map<String, Object> initBatteryCorrelation(String id)  {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("id", id);
        try {
            map.put("multiSelect", this.entityDao.findByQuery("com.broadtext.rms.basic.model.TmCustom.findBatteryCorrelationMutileSelect", queryMap));
        } catch (DaoException e) {
            e.printStackTrace();
        }
        return map;
    }

    /** (non-Javadoc)
     * <p>Title: saveBatteryCorrelation</p>
     * <p>Description:保存关联电池信息 </p>
     * @param pkId 客户id
     * @param packCode 电池id
     * @param loginUser 登录用户
     * @see com.broadtext.rms.basic.service.ITmCustomService#saveBatteryCorrelation(java.lang.String, java.lang.String, java.lang.String)
     */
    @Override
    public void saveBatteryCorrelation(String pkId, String packCode, String packCodeNot) {
        String[] packCodeNotArr = new String[]{};
        String[] packCodeArr = new String[]{};
        if(StringUtils.isNotBlank(packCodeNot)){
            packCodeNotArr = packCodeNot.split(",");
        }
        if(StringUtils.isNotBlank(packCode)){
            packCodeArr = packCode.split(",");
        }
        try {
            for (String pCode : packCodeNotArr) {
                //删除电池客户信息
                this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmCustom.updateCustomerNull", pCode);
                //删除电池所属厂区信息
                this.entityDao.deleteRecord("com.broadtext.rms.basic.model.TmCustom.deletePackFactory", pCode);
            }
            for (String pCode : packCodeArr) {
                //保存客户信息
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("pkId", pkId);
                params.put("packCode", pCode);
                this.entityDao.updateRecord("com.broadtext.rms.basic.model.TmCustom.updateCustomer", params);
            }
        } catch (DaoException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    
}
